package com.GoldMiner.ui;

import com.GoldMiner.util.DataBase;
import javax.swing.*;
import java.awt.*;

public class Register extends JFrame {
    public Register() throws Exception {
        //数据库
        DataBase db = new DataBase();
        //设置窗口属性
        this.setTitle("黄金矿工用户注册");
        this.setIconImage(new ImageIcon("src/com/GoldMiner/image/logo.png").getImage());
        this.setSize(900,600);
        this.setLocationRelativeTo(null);
        this.setAlwaysOnTop(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBackground(new Color(0xffffff));
        //面板背景
        ImageIcon loginbg = new ImageIcon("src/com/GoldMiner/image/login.jpg");
        JLabel loginlabel = new JLabel(loginbg);
        loginlabel.setBounds(0, 0, 900, 600);
        this.setBackground(new Color(0xffffff));
        //用户名输入框
        JLabel usernameLabel = new JLabel("用户名：");
        usernameLabel.setFont(new Font("幼圆", Font.BOLD, 18));//设置字体
        JTextField usernameField = new JTextField(20);
        usernameField.setFont(new Font("幼圆", Font.BOLD, 18));//设置字体
        //密码输入框
        JLabel passwordLabel = new JLabel("密码：");
        passwordLabel.setFont(new Font("幼圆", Font.BOLD, 18));//设置字体
        JPasswordField passwordField = new JPasswordField(20);
        passwordField.setFont(new Font("幼圆", Font.BOLD, 18));//设置字体
        //登录按钮
        JButton loginButton = new JButton("登录");
        JButton registerButton = new JButton("注册");
        //设置字体为幼圆
        loginButton.setFont(new Font("幼圆", Font.BOLD, 18));
        registerButton.setFont(new Font("幼圆", Font.BOLD, 18));

        // 创建自定义面板来放置用户名和密码组件
        JPanel loginPanel = new JPanel(null) {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                // 绘制背景图片
                g.drawImage(loginbg.getImage(), 0, 0, getWidth(), getHeight(), this);
            }
        };
        loginPanel.setBounds(0, 0, 900, 600);
        loginPanel.setOpaque(true); // 设置为true以确保背景色可见

        //设置按钮位置
        loginButton.setBounds(500, 420, 100, 30);
        registerButton.setBounds(300, 420, 100, 30);

        //设置用户名输入框位置
        usernameLabel.setBounds(250, 240, 80, 30);
        usernameField.setBounds(320, 240, 300, 40);

        //设置密码输入框位置
        passwordLabel.setBounds(270, 340, 80, 30);
        passwordField.setBounds(320, 340, 300, 40);

        // 添加组件到loginPanel
        loginPanel.add(usernameLabel);
        loginPanel.add(usernameField);
        loginPanel.add(passwordLabel);
        loginPanel.add(passwordField);
        loginPanel.add(loginButton);
        loginPanel.add(registerButton);
        this.add(loginPanel);
        this.setVisible(true);

        //设置布局
        this.setLayout(null);

        //设置窗口不能被调整大小
        this.setResizable(false);

        loginButton.addActionListener(e -> {
            try {
                new Login();
                this.dispose();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });
        registerButton.addActionListener(e -> {
            String password = new String(passwordField.getPassword());
            String username = usernameField.getText();
            System.out.println("username:" + username + " password:" + password);
            db.register(username,password);
            JOptionPane.showMessageDialog(this, "注册成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
            this.dispose();
            new GameMain();
        });
        //可视
        this.setVisible(true);
    }
}

